﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using System.Data.Objects;
using System.Data.Common;

namespace MINERP.DAL.CustomerManage
{
    public class ShopDAL : DALBase
    {
        /// <summary>
        /// 得到所有商户的基本信息
        /// </summary>
        /// <returns></returns>
        public static List<ShopInfo> GetAllShopInfo()
        {
            List<ShopInfo> collection = new List<ShopInfo>();

            ObjectQuery<DbDataRecord> records = DataEntites.CreateQuery<DbDataRecord>("SELECT it.shopId, it.shopName,it.mnemonicCode as ZJCode FROM Shop as it");

            ShopInfo hold = new ShopInfo();
            hold.ShopID = -1;
            collection.Add(hold);

            foreach (var item in records)
            {
                ShopInfo shop = new ShopInfo();
                shop.ShopID = (int)item["shopId"];
                shop.ShopName = (string)item["shopName"];
                shop.ShopZJM = (string)item["ZJCode"];                      //商户助记码

                collection.Add(shop);
            }

            return collection;
        }
        /// <summary>
        /// 通过商户组合条件查询订单信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<int> GetShopIDByComConidtion(criteriaShop obj)
        {
            List<int> collection = new List<int>();

            string esql = string.Empty;

            //商户名称不为空

            if (string.Compare(obj.shopName, string.Empty) != 0)
            {
                string temp = "%" + obj.shopName + "%";
                esql += " it.ShopName LIKE {0}";

                esql = string.Format(esql, "'" +temp + "'");
            }
            //联系人不为空
            if (string.Compare(obj.relationName, string.Empty) != 0)
            {                    
                string temp = "%" + obj.relationName + "%";

                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.shopKeeper LIKE {0}";
                }
                else
                {
                    esql += " it.shopKeeper LIKE {0}";
                }
                esql = string.Format(esql, "'" + temp + "'");
            }
            //电话不为空

            if (string.Compare(obj.phone, string.Empty) != 0)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.Tel1 = {0} or it.Tel2 = {0}";
                }
                else
                {
                    esql += " it.Tel1 = {0} or it.Tel2 = {0}";
                }

                esql = string.Format(esql, obj.phone);
            }
            //地址不为空

            if (string.Compare(obj.adress, string.Empty) != 0)
            {
                string temp = "'%" + obj.adress + "%'";
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.address LIKE {0}";
                }
                else
                {
                    esql += " it.address LIKE {0}";
                }
                esql = string.Format(esql, temp);
            }

            if (string.Compare(esql, string.Empty) != 0)
            {
                ObjectQuery<int> objList = DataEntites.CreateQuery<int>("SELECT VALUE it.shopId FROM Shop as it WHERE " + esql);
            

                foreach (int shopid in objList)
                {
                    collection.Add(shopid);
                }
            }

            return collection;
        }
    }
}
